package com.sunnada.edu.system.sysarea.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.Tree;
import com.sunnada.edu.system.pub.common.TreeMethods;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;
import com.sunnada.edu.system.sysarea.bean.Area;
import com.sunnada.edu.system.sysarea.service.AreaService;

/**
 * explain 区域控制类
 * 
 * @author chs 2015-5-12 version 1.0
 */
@Controller
@RequestMapping("/areaController")
public class AreaController extends BasicController {
	@Resource
	AreaService areaService;

	/** 区域树 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/areaTree")
	public void areaTree(HttpServletResponse response, HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Area.class, params);
		List list = areaService.areaTree(params);
		List treeList = new ArrayList<Tree>();
		treeList = TreeMethods.setTreeProperty(list, true);
		printInfo(response, JSONArray.toJSONString(treeList).toString());
	}

	/** 区域管理首页 zjc 2016-08-11 */
	@RequestMapping("/area")
	public String area(HttpServletRequest request) {
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/sysarea/area";
	}

	/** 区域增加 zjc 2016-08-10 */
	@RequestMapping("/areaAddshow")
	public String areaAddshow(HttpServletRequest request) {
		return "system/sysarea/area_add";
	}

	/** 区域保存功能 zjc 2016-08-10 */
	@RequestMapping("/areaSave")
	public void areaSave(HttpServletResponse response, HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Area.class, params);
		String recId = params.get("recId");
		if (Utils.isNull(recId)) {
			msg = areaService.areaAdd(params);
		} else {
			msg = areaService.areaUpdate(params);
		}
		printInfo(response, msg);
	}

	/** 区域修改界面 zjc 2016-08-10 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/areaUpdateShow")
	public String areaUpdateShow(HttpServletRequest request, HttpServletResponse repoResponse) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Area.class, params);
		Map area = areaService.getAreaByRecId(params);
		request.setAttribute("area", area);
		return "system/sysarea/area_update";
	}

	/** 区域修改功能 zjc 2016-08-10 */
	@RequestMapping("/areaDelete")
	public void areaDelete(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = areaService.areaDel(params);
		printInfo(response, msg);
	}

	@RequestMapping("/areaTreebig")
	public void areaTreebig(HttpServletResponse response, HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Area.class, params);
		String chrild = areaService.areaTreebig(params);
		printInfo(response, chrild);
	}

	/** 区域查询 zjc 2015-05-13 */
	@RequestMapping("/areaSearch")
	public void areaSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Area.class, params);
		Page page = new Page();
		page.setRowCount(areaService.getAreaCots(params));
		page.setPageMap(request, page, params);
		List<?> areaList = areaService.findAreas(params);
		JSONObject json = new JSONObject();
		json.put("list", areaList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 根据区域areaCode查询区域信息 zjc 2015-05-14 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/areaByAreacode")
	public void areaByAreacode(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String areaCode = request.getParameter("areaCode");
		params.put("areaCode", areaCode);
		Map area = areaService.areaByAreacode(params);
		JSONObject json = new JSONObject();
		json.put("area", area);
		printInfo(response, json);
	}

	/** 区域启用功能 zjc 2015-5-26 */
	@RequestMapping("/areaEnable")
	public void areaEnable(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = areaService.areaEnable(params);
		printInfo(response, msg);
	}

	@RequestMapping("/areaDisable")
	public void areaDisable(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", recIds);
		msg = areaService.areaDisable(params);
		printInfo(response, msg);
	}

}
